import { asyncRoutes8 } from '@/router/index'
import getPageTitle from '@/utils/get-page-title'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import router from './router'
import store from './store'
NProgress.configure({ showSpinner: false })
// 白名单 : 不需要token就可直接访问的页面
const whiteList = ['/login', '/404']

// 导航守卫 路由变化都会调用 回调函数 ===> 导航守卫
router.beforeEach(async(to, from, next) => {
  // 启动页面跳转的进度条   NProgress.start() // 启动进度条  NProgress.done() //进度条结束
  NProgress.start()
  // 获取token
  const token = store.state.user.token
  // const userId = store.state.user.userInfo.userId
  const userId = store.getters.userId
  if (token) {
    // 是否去登录页
    if (to.path === '/login') {
      // console.log('你已经登录了,就不能去login,转到主页')
      // 跳转主页
      next('/')
      NProgress.done()
    } else {
      if (!userId) {
        // 有token要正常访问首页 ,发请求拿信息:调用action
        const res = await store.dispatch('user/getUserProfile')
        const menus = res.data.roles.menus
        const filterRoutes = asyncRoutes8.filter((item) => {
          const name = item.children[0].name
          return menus.includes(name)
        })
        console.log(
          '路由守卫',
          filterRoutes,
          asyncRoutes8,
          res.data.roles.menus
        )
        // 根据权限信息动态添加路由
        // (1)可以让我们在地址栏中访问页面
        // 让404在整个路由配置的最后面
        filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
        router.addRoutes(filterRoutes)
        // (2)把动态的路由添加到vuex
        store.commit('menu/updateMenuList', filterRoutes)

        next({
          ...to,
          replace: true
        })
      } else {
        next()
      }
    }
  } else {
    // 没有登录,只能去白名单(不需要token就可以访问的页面)
    // 是否白名单 : /login
    if (whiteList.includes(to.path)) {
      // 放行
      next()
    } else {
      // 去登录页
      next('/login')
      NProgress.done()
    }
  }
})
// 路由守卫可以写多个
router.beforeEach((to, from, next) => {
  console.log('守卫2', to.meta.title)
  document.title = getPageTitle(to.meta.title)
  next()
})
router.afterEach((to, from) => {
  // console.log('')
  NProgress.done()
})
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })
